package com.xinchengxd.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.core.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

@MapperScan("com.xinchengxd.order.mapper")
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /*设置mq邮件交换机*/
    @Bean
    public Exchange directExchange(){
        return new DirectExchange("yxc_mail_exchange");
    }

    /*设置mq死信交换机*/
    @Bean
    public Exchange payoutExchange(){
        return new DirectExchange("yxc-payout-exchange");
    }


    /*设置mq邮件队列*/
    @Bean
    public Queue queue(){
        return new Queue("yxc_mail_queue");
    }

    /*设置mq支付ttl队列，设置过期死信交换机，死信队列*/
    @Bean
    public Queue payQueue(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl",30000); //设置消息30s过期
        arguments.put("x-dead-letter-exchange","yxc-payout-exchange"); //设置死信交换机
        arguments.put("x-dead-letter-routing-key","order-payout"); //设置死信队列
        return new Queue("yxc_pay_queue",true,false,false,arguments);
    }

    /*设置mq死信队列*/
    @Bean
    public Queue payoutQueue(){
        return new Queue("yxc_payout_queue");
    }

    /*设置邮件队列绑定*/
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(directExchange()).with("order_mail").noargs();
    }
    /*设置支付队列绑定*/
    @Bean
    public Binding payBinding(){
        return BindingBuilder.bind(payQueue()).to(directExchange()).with("order_pay").noargs();
    }

    /*设置死信队列绑定*/
    @Bean
    public Binding payoutBinding(){
        return BindingBuilder.bind(payoutQueue()).to(payoutExchange()).with("order-payout").noargs();
    }
}

